/**
 * @class   wosh::network::libsockets::SocketUdpLS
 * @brief   The class provides a UDP socket.
 *          UDP (user datagram protocol) is a lightweight, unreliable,
 *          datagram-oriented, connectionless protocol.
 *
 ****************************************************************************
 * @version 0.8.151 $Id: SocketUdpLS.h 2850 2010-08-06 18:49:18Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Network_libsockets_SocketUdpLS_H__
 #define __WOSH_Network_libsockets_SocketUdpLS_H__

 #include <core/Thread.h>
 #include <framework/network/SocketUdp.h>


namespace Sockets {
	class SocketHandler;
};
struct sockaddr;

namespace wosh {
 namespace network {
  namespace libsockets {

class SocketUdpLSWrapper;

class SocketUdpLS :		private ThreadImpl,
						public wosh::network::SocketUdp
 {
	WOSH_CLASSNAME(wosh::network::libsockets::SocketUdpLS)
	WOSH_CLASS_ABSTRACT(false)


	public:
		/**
		 * @brief   Empty constructor, init vars.
		 */
		SocketUdpLS();
		/**
		 * @brief   Empty destructor, may (synch) unbind socket (stop internal thread)
		 */
		virtual ~SocketUdpLS();


	public:

		/**
		 * @brief  Bind the UDP socket.
		 * @param timeout [in] the timeout of the operation, 0 for total asynch mode, no timeout by default
		 * @return WRET_ERR_WRONG_STATE when already binded; WRET_ERR_INTERNAL if
		 *         the binding failed (or timed-out); WRET_OK else
		 * @note   this call will start synchronously the internal thread
		 * @see    setAddressPort()
		 */
		virtual WRESULT bindSocket( unsigned long timeout = LIMIT_ULONG_MAX );

		/**
		 * @brief  UnBind the UDP socket.
		 * @param timeout [in] the timeout of the operation, 0 for total asynch mode, no timeout by default
		 * @return WRET_ERR_WRONG_STATE when not binded; WRET_ERR_INTERNAL if
		 *         the binding failed (or timed-out); WRET_OK else
		 * @note   this call will stop synchronously the internal thread
		 * @see    bindSocket()
		 */
		virtual WRESULT unbindSocket( unsigned long timeout = LIMIT_ULONG_MAX );

		/**
		 * @brief  Send an UDP datagram to remote host.
		 * @param [in] destination target address:port (such as '192.168.0.255:8787')
		 * @param [in] data the block of bytes to be sent (no marshalling)
		 * @param [in] size number of bytes of 'data' argument
		 * @return number of sent bytes
		 */
		virtual int sendDatagram( const std::string& destination, const char* data, int size );


		virtual bool isBinded() const;


/** @name Internal vars and methods
 * @{
 ******************************************************************************/
	protected:

		bool initializingThread();
		void runThread();
		void exitingThread();

		Sockets::SocketHandler* sHandler;
		SocketUdpLSWrapper* socketWapper;

		friend class SocketUdpLSWrapper;

		void OnRawData(const char *p,size_t l, struct sockaddr *sa_from, int sa_len );
//@}

}; // class def

  }; // namespace libsockets
 }; // namespace network
}; // namespace wosh

#endif //__WOSH_Network_libsockets_SocketUdpLS_H__
